<!doctype html>
<html>
<head>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
<title>主播投票</title>
<link href="../../css/newStyle.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../../js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/gbRst.js"></script>
<script type="text/javascript" src="../../js/utils.js"></script>
<script type="text/javascript" src="../../js/openId.js"></script>
<script type="text/javascript" src="../../js/header&footer.js"></script>
<script type="text/javascript" src="../../js/qmsk-1.1.js"></script>
<script type="text/javascript">
$(function() {
	showAllVote();
});
var isFind=false;
var localip;//定义本机ip
//_openId="00002";
//获得本机ip
 function getip(){
	var url="../../../wechat/getlocalIP";
	$.ajax({
		type : "get",
		async:false,
		datatype : "json",
		url : url,
		success : function(data) {
			var rst = JSON.parse(data);
			//alert(rst.result)
			localip=rst.result;
		}	
	}); 
} 
//获得全部投票的ip
/* function getAllvoteip(){
	var url="../../../applyvoteip/find/applyvoteipListwx";
	qmsk.get(url, function (data){
		//alert(data.getValue("ipaddress",0));
		for(var i=0;i<data.length;i++){
			//alert("本地IP"+_openId+"==数据库中的ip"+data.getValue("openid",i));
			if(_openId==data.getValue("openid",i)){
				isFind=true;
			}
		}
	});
}
 */

//获得全部投票的ip
function getipbyapplyid(CurrentVotenum,upid){
	//alert("applyid=="+_openId);
	var url="../../../applyvoteip/find/applybyidwx";
	$.ajax({
		type : "post",
		async:false,
		data:{"data":upid,"id":_openId},
		datatype : "json",
		url : url,
		success : function(data) {
			var data=JSON.parse(data);
			if(data.result.len>0){
				alert("你已经参与过投票，谢谢关注!")
			}else{
				immediatelyVote(CurrentVotenum,upid);
			}
		}	
	});
}



//判断ip是否以已经抽过票了
/* function isExists(CurrentVotenum,upid){
	 if(isFind==true){
		
		//$(".pay").attr("disabled","disabled");
	}else{
		immediatelyVote(CurrentVotenum,upid);
	} 
	
} */
//添加已经投票的用户openid进入数据库
function insertip(ip,applyid){
	//alert("openid==="+_openId)
	var heads = new Array("ipaddress","applyid","openid");
	var datas = new Array(ip,applyid,_openId);
	var parser = new Object();
	//alert("本地ip为"+ip+"用户id"+applyid);
	//alert("图片介绍=="+uimagetext+"==是否参与"+uparticipation+"==赛区"+utype+"==更新路径"+imageurl)
	var jsonParserStr = null;//定义空头字符串
	if (IsNotEmpty(datas)) {
		addToParser(heads, datas, "add", parser);
		jsonParserStr = JSON.stringify(parser);
	}
	var url = "../../../applyvoteip/add/insertip";
	$.post(url, {
		"data" : jsonParserStr
	}, function(data) {
		var dt = JSON.parse(data);
		if(dt.result>0){
			alert("投票成功");
			getAllvoteip();
			//history.go(0);
			showAllVote();
		}else{
			alert("添加openid异常");
		}
		console.log(data);
		//saveOwnShopIds();
	});
}
//显示全部参与投票信息
function showAllVote(){
	var selectType=$("#selectType").val();
	var params = {
		'type' : selectType
	}
	var data = findInfoAll("../../../apply/find/applyListwx",
			params);
	if (IsNotEmpty(data) && data.state == 0) {
		//var dt = JSON.parse(data);
		//alert(dt.result.data.length)
		$("#allInfo").empty();
		for ( var i = 0; i < data.result.data.length; i++) {
			//alert("返回结果为"+getHeadIndexValue(i, data, "participation"))
			//alert(getHtmlString(getHeadIndexValue(i, data, "name")))
			var htm = "<dl>"
				htm += "<dt><img src='"+getHtmlString(getHeadIndexValue(i, data, "imageUrl") )+"' onclick=\"redirectUrl('voteNow.html?id="+getHtmlString(getHeadIndexValue(i, data, "id"))+"&openId="+_openId+"')\"></dt>"
				htm+="<dd>"
				htm+=" <p><span>姓名：</span>"+getHtmlString(getHeadIndexValue(i, data, "name"))+"</p>"
				var votenum=getHtmlString(getHeadIndexValue(i, data, "votenum"));
				htm+="<p><span>当前票数：</span><span class='red'>"+votenum+"</span></p>"
				htm+="</dd>"
				htm+="<dd class='allBtn'>"
				htm+=" <input type='button' class='del' value='查看详情' onclick=\"redirectUrl('voteNow.html?id="+getHtmlString(getHeadIndexValue(i, data, "id"))+"&openId="+_openId+"')\" ></br>"
				htm+="<input type='button' class='pay voteOver' onclick=\"getipbyapplyid("+getHtmlString(getHeadIndexValue(i, data, "votenum"))+","+getHtmlString(getHeadIndexValue(i, data, "id"))+")\" value='立即投票' >"
				htm+="</dd>"
				htm += "</dl>"
				$("#allInfo").append(htm);
		}
		console.log(data);
		//'redirectUrl('voteNow.html')'
	}
}
function immediatelyVote(CurrentVotenum,upid){
	//alert("进入修改当前票数为=="+CurrentVotenum+"id为=="+upid)
	var heads = new Array("votenum","id");
	var datas = new Array(parseInt(CurrentVotenum)+1,upid);
	var parser = new Object();
	//alert("图片介绍=="+uimagetext+"==是否参与"+uparticipation+"==赛区"+utype+"==更新路径"+imageurl)
	var jsonParserStr = null;//定义空头字符串
	if (IsNotEmpty(datas)) {
		addToParser(heads, datas, "edit", parser);
		jsonParserStr = JSON.stringify(parser);
	}
	var url = "../../../apply/up/updateVotenum";
	$.post(url, {
		"data" : jsonParserStr
	}, function(data) {
		var dt = JSON.parse(data);
		if(dt.result>0){
			insertip(localip,upid);
			
			showAllVote();
		}else{
			alert("投票异常")
		}
		console.log(data);
		//saveOwnShopIds();
	});
}


function findInfoAll(url,params){
	var rst;
	$.ajax({
		type : "get",
		async:false,
		data:params,
		datatype : "json",
		url : url,
		success : function(data) {
			 rst = JSON.parse(data);
			 if(IsEmpty(rst)){
				 alert('查询出错');
			 }else{
			 if(rst.state==1){
				 alert('查询出错');
			 }
			 }
		}	
	});
	return rst;
}
</script>
</head>

<body>
<div class="wrapper">
  <div class="container">
  	<div class="checkDiv" style="width:310px;margin:5px auto 0;"><select  id="selectType" onchange="showAllVote()"><option value="0">高校赛区</option><option value="1">大众赛区</option></select></div>
    <div class="hostImg">
      <div id='allInfo' >
       <!--  <dl>
          <dt><img src="http://www.scmytv.com:4280/html/user/283/1.png">
            <div class="pendant"><span>已投票</span></div>
          </dt>
          <dd>
            <p><span>姓名：</span>郭琴</p>
            <p><span>当前票数：</span><span class="red">0</span></p>
          </dd>
          <dd class="allBtn">
            <input type="button" class="del"  value="查看详情" onclick="redirectUrl('voteNow.html')">
            <br>
            <input type="button" class="pay voteOver" value="立即投票">
          </dd>
        </dl>
        <dl>
          <dt><img src="http://www.scmytv.com:4280/html/user/283/1.png">
          </dt>
          <dd>
            <p><span>姓名：</span>郭琴</p>
            <p><span>当前票数：</span><span class="red">0</span></p>
          </dd>
          <dd class="allBtn">
            <input type="button" class="del" value="查看详情" onclick="redirectUrl('voteNow.html')">
            <br>
            <input type="button" class="pay" value="立即投票">
          </dd>
        </dl>
        <dl>
          <dt><img src="http://211.149.190.104:80/user/256/user.jpg">
          </dt>
          <dd>
            <p><span>姓名：</span>郭琴</p>
            <p><span>当前票数：</span><span class="red">0</span></p>
          </dd>
          <dd class="allBtn">
            <input type="button" class="del" value="查看详情">
            <br>
            <input type="button" class="pay" value="立即投票">
          </dd>
        </dl>
        <dl>
          <dt><img src="http://211.149.190.104:80/user/256/user.jpg">
            <div class="pendant"><span>已投票</span></div>
          </dt>
          <dd>
            <p><span>姓名：</span>郭琴</p>
            <p><span>当前票数：</span><span class="red">0</span></p>
          </dd>
          <dd class="allBtn">
            <input type="button" class="del" value="查看详情">
            <br>
            <input type="button" class="pay voteOver" value="立即投票">
          </dd>
        </dl> -->
      </div>
      <div class="fc"></div>
    </div>
  </div>
</div>
</body>
</html>
